from flask import Flask
from config import config_map
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()


def create_app(config_name):

    app = Flask(__name__)

    config = config_map.get(config_name)
    app.config.from_object(config)

    db.init_app(app)

    # 获取蓝图应用
    from flask_shop.user import user_bp
    app.register_blueprint(user_bp)

    # 获取menu蓝图对象
    from flask_shop.menu import menu_bp
    from flask_shop.menu import menu_bp
    app.register_blueprint(menu_bp)

    # 获取role蓝图对象
    from flask_shop.role import role_bp
    app.register_blueprint(role_bp)

    from flask_shop.category import cate_bp, attr_bp
    app.register_blueprint(cate_bp)
    app.register_blueprint(attr_bp)

    from flask_shop.product import product_bp
    app.register_blueprint(product_bp)

    from flask_shop.order import order_bp
    app.register_blueprint(order_bp)

    return app
